<?php
/**
 * User: Mykola B.
 * Date: 3/1/13
 * Time: 6:51 PM
 */
namespace models {

    class UserModel extends BaseModel {

        protected static $fields = array(
            'id',
            'username',
            'password',
            'create_time'
        );

        // used for serialization
        private $id;

        public function __construct() {
            return parent::__construct('users');
        }

        public function getByName($name) {
            $success =  $this->mapper->load(array('username=?', $name));
            if ($success) $this->setId();
            return $success;
        }

        public function getById($id) {
            $success =  $this->mapper->load(array('id=?', $id));
            if ($success) $this->setId();
            return $success;
        }

        public function __sleep() {
            return array('id');
        }

        public function __wakeup() {
            parent::__construct('users');
            $this->getById($this->id);
        }

        private function setId() {
            $this->id = $this->mapper->id;
        }
    }

}
